<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/1/27
 * Time: 17:50
 */

namespace app\admin\controller;


use app\common\controller\Backend;

class Usernote extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('UserNote');
    }
    /**
     * 用户解签列表
     * @param string $ids
     * @return string
     */
    public function index($ids=''){
        if ($this->request->isAjax()) {
            $this->relationSearch = true;
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $note_ids_list = model('Note')
                ->where(array('temple_id' => $ids))
                ->field('id')
                ->select();
            $note_ids = [];
            foreach ($note_ids_list as $k => $v) {
                $note_ids[] = $v['id'];
            }
            $total = model('UserNote')
                ->with('user,note')
                ->where($where)
                ->where('note_id','in',$note_ids)
//                ->order($sort, $order)
                ->count();

            $list = model('UserNote')
                ->with('user,note')
                ->where($where)
                ->where('note_id','in',$note_ids)
//                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            foreach ($list as $key=>&$value){
                $value['temple_name'] = model('Temple')->where(array('id'=>$value['note']['temple_id']))->value('name');
                $solve_note_ids = $value['note']['solve_note_ids'];
                $solve_note_list = model('SolveNote')->where('id','in',$solve_note_ids)->select();
                $solve_note_name_list =[];
                foreach ($solve_note_list as $k=>$v){
                    $solve_note_name_list[]=$v['name'];
                }
                $content_list = model('Content')
                    ->where(array('note_id'=>$value['note']['id']))
                    ->select();
                $content_array = [];
                foreach ($content_list as $k=>$content){
                    $content_array[]=$content['content'];
                }
                $value['solve_note_text'] = implode(',', $solve_note_name_list);
                $value['content_text'] = implode(',', $content_array);
            }
            return json($result);
        }
        $this->assignconfig('temple_id',$ids);
        return $this->view->fetch();
    }
}